import Vue from 'vue'
import Vuex from 'vuex'
import config from '../config/config.js'
Vue.use(Vuex)

import VuexPersistence from 'vuex-persist'

const vuexLocal = new VuexPersistence(
{
	storage: window?.localStorage || localStorage,
}
)

const store = new Vuex.Store({
	state: {
		publicKey: uni.getStorage('publicKey') || '',
		systemSupport: {},
		userInfo: {},
		//律师基础信息
		lawyerBaseInfo: {},
		//律师角色信息
		lawyerRoles: [],
		//律师上下线信息
		lawyerStatus: {},
		systemInfo: {
			name: '',
			subTitle: '',
			hasPortal: true,
			portalPath: '',
			copyRight: '',
			logoPath: '',
			headerBgPath: '',
			theme: '',
			systemCode: 'agility'
		},
		// 网络状态改变
		netWorkStatus: true,
		networkType: undefined
	},
	mutations: {
		reset(state, data) {
			state.userInfo = {}
			state.lawyerBaseInfo = {}
			state.lawyerRoles = []
			state.lawyerStatus = {}
			state.systemInfo = {}
		},
		changeNetWorkType(state, data) {
			state.networkType = data // 存vuex
		},
		changeNetWork(state, data) {
			state.netWorkStatus = data // 存vuex
		},
		setPublicKey(state, data) {
			state.publicKey = data // 存vuex
			uni.setStorage({
				key: "publicKey",
				data: data
			})
		},
		SET_SYSTEMINFO(state, payload) {
			state.systemInfo = payload
			uni.setStorage({
				key: "systemInfo",
				data: payload
			})
		},
		SET_SYSTEMINFO_PARAMTER(state, payload) {
			// 直接异常抛掉
			if (payload.key == undefined || payload.key == null) throw Error("please check data key!")
			state.systemInfo[payload.key] = payload.value
			uni.setStorage({
				key: "systemInfo",
				data: state.systemInfo
			})
		},
		SET_SYSTEMSUPPORT(state, payload) {
			state.systemSupport = payload
			uni.setStorage({
				key: "systemSupport",
				data: payload
			})
		},
		SET_SYSTEMSUPPORT_PARAMTER(state, payload) {
			// 直接异常抛掉
			if (payload.key == undefined || payload.key == null) throw Error("please check data key!")
			state.systemSupport[payload.key] = payload.value
			uni.setStorage({
				key: "systemSupport",
				data: state.systemSupport
			})
		},
		SET_USER(state, payload) {
			state.userInfo = payload
			uni.setStorage({
				key: "userInfo",
				data: payload
			})
		},
		SET_USER_PARAMTER(state, payload) {
			// 直接异常抛掉
			if (payload.key == undefined || payload.key == null) throw Error("please check data key!")
			state.userInfo[payload.key] = payload.value
			uni.setStorage({
				key: "userInfo",
				data: state.userInfo
			})
		},
		SET_LAWYER_BASEINFO(state, payload) {
			state.lawyerBaseInfo = payload
			uni.setStorage({
				key: "lawyerBaseInfo",
				data: payload
			})
		},
		SET_LAWYER_ROLES(state, payload) {
			state.lawyerRoles = payload
			uni.setStorage({
				key: "lawyerRoles",
				data: payload
			})
		},
		SET_LAWYER_STATUS(state, payload) {
			state.lawyerStatus = payload
			uni.setStorage({
				key: "lawyerStatus",
				data: payload
			})
		}
	},
	getters: {
		networkType: state => state.networkType,
		netWorkStatus: state => state.netWorkStatus,
		publicKey: state => state.publicKey,
		systemInfo: state => state.systemInfo,
		systemCode: state => state.systemInfo?.systemCode || 'agility',
		systemSupport: state => state.systemSupport || {},
		lawyerBaseInfo: state => state.lawyerBaseInfo || {},
		lawyerRoles: state => state.lawyerRoles || [],
		lawyerStatus: state => state.lawyerStatus || {},

		userInfo: state => state.userInfo || {},
		name: state => state.userInfo?.name,
		account: state => state.userInfo?.account || 'guest',
		accountType: state => state.userInfo.accountType,
		avatar: state => state.userInfo?.avatar,
		isAdmin: state => state.userInfo?.isAdmin,
		systemRoles: state => state.userInfo?.systemRoles,
	},
	actions: {
		updatePublicKey(context, data) {
			context.commit("setPublicKey", data)
		},
		// 登录操作
		async updateSystemInfo(context, data) {
			// 触发mutation，存储data
			context.commit("SET_SYSTEMINFO", data)
		},
		async updateSystemInfoParams(context, data) {
			// 触发mutation，存储data
			if (data instanceof Array) {
				data.forEach(item => {
					context.commit("SET_SYSTEMINFO_PARAMTER", item)
				})
			} else {
				context.commit("SET_SYSTEMINFO_PARAMTER", data)
			}
		},
		async updateSystemSupport(context, data) {
			// 触发mutation，存储data
			context.commit("SET_SYSTEMSUPPORT", data)
		},
		async updateUserInfo(context, data) {
			// 触发mutation，存储data
			context.commit("SET_USER", data)
		},
		async updateUserInfoParams(context, data) {
			// 触发mutation，存储data
			if (data instanceof Array) {
				data.forEach(item => {
					context.commit("SET_USER_PARAMTER", item)
				})
			} else {
				context.commit("SET_USER_PARAMTER", data)
			}
		},
		async updatelawyerInfo(context, data) {
			// 触发mutation，存储data
			context.commit("SET_LAWYER_BASEINFO", data)
		},
		async updateLawyerStatus(context, data) {
			// 触发mutation，存储data
			context.commit("SET_LAWYER_STATUS", data)
		},
		// 退出操作
		async resetAll(context) {
			context.commit("reset")
			// uni.removeStorageSync(config.token)
			uni.clearStorage();
		}
	},
	plugins: [vuexLocal.plugin],
})

export default store